//
//  CZEmoticonPackage.swift
//  表情包数据
//
//  Created by 名品导购网MPLife.com on 2017/12/13.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit
import YYModel
//表情包模型
class CZEmoticonPackage: NSObject {
    //表情包的分组名
    @objc var groupName:String?
    //背景图片名称
    @objc var bgImageName:String?
    //表情包目录 从目录下加载info.plist 可以创建表情模型数组
    @objc var directory:String?{
        didSet{
            //当设置目录时 从目录下加载info.plist
            guard let directory = directory,let path = Bundle.main.path(forResource: "HMEmoticon.bundle", ofType: nil),let bundle = Bundle(path: path), let infoPath = bundle.path(forResource: "info.plist", ofType: nil, inDirectory: directory),let array = NSArray(contentsOfFile: infoPath) as? [[String:String]],let model = NSArray.yy_modelArray(with: CZEmoticon.self, json: array) as? [CZEmoticon]
                 else {
                return
            }
            //遍历model设置每一个表情符号的目录
            for m in model {
                m.directory = directory
            }
            //设置表情模型数组
            emoticons += model
        }
    }
    //蓝架子啊表情模型数组
    //使用懒加载可以避免后续解包
    lazy var emoticons = [CZEmoticon]()
    var numberOfPages:Int{
//        return emoticons.count/21
        return (emoticons.count - 1)/21 + 1
    }
    //从懒加载的表情包中按照page截取最多20个表情模型的数组
    //例如有26个表情
    //page == 0  返回0~19个模型
    //page == 1 返回20~25个模型
    func emoticon(page:Int) -> [CZEmoticon] {
        //
        let count = 20
        let location = page*count
        var length = count
        if location + length > emoticons.count {
            length = emoticons.count - location
        }
        
        //截取子数组
        let range = NSRange(location: location , length: length)
        let subArray = (emoticons as NSArray).subarray(with: range)
        return subArray as! [CZEmoticon]
    }
    override var description: String{
        return yy_modelDescription()
    }
}



















